<?php

class News_m extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    function get_by_cat($limit,$offset,$cat){
	$this->db->where('category',$cat);
	$this->db->limit($limit,$offset);
	$query = $this->db->get('news');
	return $query->result();
    }
    function getall($limit, $offset, $id = 0) {
        if (!is_numeric($id) || $id < 0)
            return null;

        if ($id == 0) {
            if ($limit == 0) {
                $this->db->order_by('id', 'DESC');
                
                $query = $this->db->get('news');
                $query = $query->result();
                foreach($query as &$q){
                    $this->db->where('id',$q->category);
                    $q->catalogue = $this->db->get('category')->first_row()->name;
                }
                return $query;
            } else {
                
                $this->db->order_by('id', 'DESC');
                $this->db->limit($limit, $offset);
                
                $query = $this->db->get('news')->result();
                foreach($query as &$q){
                    $this->db->where('id',$q->category);
                    $qt = $this->db->get('category')->first_row();
                    if($qt)
                    $q->catalogue = $qt->name;
                    else {
                        echo $this->db->last_query();
                        die();
                    }
                }
                return $query;
            }
        } else {
            $this->db->where('id', $id);
            $query = $this->db->get('news');
            return $query->result();
        }
    }
    
    function get_cat(){
        $this->db->where('parent >',0);
        return $this->db->get('category')->result();
    }
    
    function get_news($id=0) {
        if (!is_numeric($id) || $id < 0)
            return null;

        if ($id == 0) {
            $this->db->order_by('id', 'DESC');
            $this->db->where('status',1);
            $query = $this->db->get('news');
            $query = $query->result();
                foreach($query as &$q){
                    $this->db->where('id',$q->category);
                    $q->catalogue = $this->db->get('category')->first_row()->name;
                }
                return $query;
        } else {
            $this->db->where('id', $id);
            $query = $this->db->get('news');
            if ($query->num_rows > 0)
                $query = $query->first_row();
        }
    }

    function count($id){
        if($id <0 )
            return null;
        
        if($id > 0){
            //get parent catalogue
          
            $this->db->where('id',$id);
            $query = $this->db->get('catalogue');
            $data['parent'] = $query->result();
            //get all sub catalogue by parent id
            $count = 0;
            if(count($data['parent'])<=0)
                return 0;
            $arr = $data['parent'][0];
            if($arr->parent_id ==0){
                $this->db->order_by('order');
                $this->db->where('language',$lang);
                $this->db->where('parent_id ',$id);
                $this->db->limit(1);
                $data['cate'] = $this->db->get('catalogue')->result(); 
                
                //die(print_r($data['cate']));
                //get news
                if(count($data['cate'])>0){
                    $temp = $data['cate'][0];
                    $this->db->where('catalogue_id',$temp->id);
                }else{
                    $data['cate'] = $data['parent'];
                    $this->db->where('catalogue_id',$arr->id);
                }
                $this->db->where('status',1);
                $query1 = $this->db->get('news');
                return   $query1->num_rows();
            }else{
  
                $this->db->order_by('news.id','DESC');
                $this->db->where_in('catalogue_id',$id);
                $this->db->where('status',1);
                $query1 = $this->db->get('news');
                return $query1->num_rows();
            }
        }
        //return $count;
    }

    function gethot(){
        $this->db->select('news.*,catalogue.alias as catalogue');
        $this->db->join('catalogue', 'news.catalogue_id = catalogue.id');
        $this->db->order_by('id', 'DESC');
        $this->db->where('special', 1);
        
        $this->db->where('catalogue.language', $lang);
        $this->db->limit(10);
        $query = $this->db->get('news');
        return $query->result();
    }

    function get_news_catalogue($id=0,$order=0,$limit=0,$offset=0) {
        if ($id < -1)
            return null;
        
        $this->db->select('news.*,catalogue.name as catalogue');
        $this->db->join('catalogue','news.catalogue_id = catalogue.id');
        if($id == 0)
            $this->db->where('special',1);
        if($id > 0)
            $this->db->where('catalogue_id',$id);

        if($order == 0)
            $this->db->order_by('news.id', 'DESC');
        else if($order == 1)
            $this->db->order_by('news.id','ASC');
        else
            $this->db->order_by('news.order');
        
        
        $this->db->limit($limit,$offset);
        $query = $this->db->get('news');
        return $query->result();
    }

    function add($data) {
       // if ($data['title'] == '' || $data['content'] == '' || $data['alias'] == '')
           // return false;

        //$data['title'] = $this->db->escape_str($data['title']);
        //$data['alias'] = $this->db->escape_str($data['alias']);

        //$data['date_created'] = time();
        if ($this->db->insert('news', $data))
            return $this->db->insert_id();
        else
            return false;
    }

    function update($id, $data) {
        //if ($data['title'] == '' || $data['content'] == '' || $data['alias'] == '')
         //   return false;

        //$data['title'] = $this->db->escape_str($data['title']);
        // $data['alias'] = $this->db->escape_str($data['alias']);
        $data['date_modified'] = time();

        $this->db->where('id', $id);
        if ($this->db->update('news', $data))
            return true;
        else
            return false;
    }

    function delete($ids) {
        if (is_array($ids)) {
            foreach ($ids as $id) {
                $this->db->where('id', $id);
                $this->db->delete('news');
            }
            return true;
        }else {
            if (!is_numeric($ids))
                return false;
            $this->db->where('id', $ids);
            if ($this->db->delete('news'))
                return true;
            else
                return false;
        }
    }

    function search_result($keyword, $limit=0, $offset=0) {

        if ($limit == 0) {
            $this->db->order_by('id', 'DESC');
            $this->db->select('news.*,catalogue.name AS catalogue');
            $this->db->join('catalogue', 'news.catalogue_id=catalogue.id');
            $this->db->like('title', $keyword);
            $query = $this->db->get('news');
            return $query->result();
        } else {
            $this->db->order_by('id', 'DESC');
            $this->db->limit($limit, $offset);
            $this->db->select('news.*,catalogue.name AS catalogue');
            $this->db->join('catalogue', 'news.catalogue_id=catalogue.id');
            $this->db->like('title', $keyword);
            $query = $this->db->get('news');
            return $query->result();
        }
    }

    function count_for_search($keyword) {
        $this->db->like('title', $keyword);
        $query = $this->db->get('news');
        return $query->num_rows();
    }
    
    function count_catalogue($id=0){
        if($id == 0)
            $this->db->where('special',1);
        else if($id > 0)
            $this->db->where('catalogue_id',$id);
        $query = $this->db->get('news');
        return $query->num_rows();
    }
    


    function get_showed($lang) {
        $this->db->where('show', 1);
        $this->db->where('parent_id !=', 0);
        $this->db->where('language', $lang);
        $query = $this->db->get('catalogue');
        $result = array();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $q) {
                $this->db->where('catalogue_id', $q->id);
                $this->db->where('status',1);
                //$this->db->where('special !=',1);
                $this->db->select_max('id');
                $nq = $this->db->get('news');
                if ($nq->num_rows() > 0) {
                    $new = $nq->first_row();
                    $this->db->where('id', $new->id);
                    $q1 = $this->db->get('news');
                    if ($q1->num_rows() == 0) continue;
                    $news = $q1->first_row();
                    $news->catalogue = $q->name;
                    $news->catalogue_alias = $q->alias;
                    $result[] = $news;
                }
            }
            return $result;
        }
        else
            return 0;
    }

    function get_detail($id) {
        if (!is_numeric($id))
            return 0;
        $this->db->where('id', $id);
        $query = $this->db->get('news');
        if (!($query->num_rows > 0))
            return 0;
        $new = $query->first_row();
        $this->db->where('id', $new->catalogue_id);
        $cata = $this->db->get('catalogue')->first_row();
        $new->catalogue = $cata->name;
        $new->cat_alias = $cata->alias;
        return $new;
    }

    function get_related($cat_id = 0,$date_created=0, $limit = 10) {
        if ($cat_id == 0) {
            $this->db->order_by('id', 'desc');
            $this->db->where('status',1);
            $this->db->where('date_created <',$date_created);
            $this->db->limit($limit);
            return $this->db->get('news')->result();
        } else {
            $this->db->order_by('id', 'desc');
            $this->db->where('catalogue_id', $cat_id);
            $this->db->where('date_created <',$date_created);
            $this->db->where('status',1);
            $this->db->limit($limit);
            return $this->db->get('news')->result();
        }
    }

}
